package com.core.family.gen.service.gen;

import com.core.common.utils.SqlColumnConvertUtil;
import com.core.family.gen.config.GenConfManger;
import com.core.family.gen.domain.po.TableBeanPo;
import com.core.family.gen.domain.po.TableColumnPo;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @description:
 * @author: honglei
 * @time: 2021/3/23 0023 17:16
 */
public interface IGenHandler {


    void setConfig(GenConfManger config);

    /*获取表集合*/
    List<TableBeanPo> tablesList() throws Exception;

    /***
     * 获取表字段
     * @param table
     * @return
     * @throws Exception
     */
    List<TableColumnPo> getColumns(String table) throws Exception;

    /*指定表代码生成*/
    default boolean genCode(List<TableBeanPo> tableNames) throws Exception {
        for (TableBeanPo tableName : tableNames) {
            genCode(tableName);
        }
        return true;
    }

    /*指定表代码生成*/
    boolean genCode(TableBeanPo columnBean) throws Exception;


    default String tableToJava(String tableName, String tablePrefix) {
        return tableToJava(tableName, tablePrefix, false);
    }

    /**
     * 表名转java 类名
     *
     * @param tableName     表名
     * @param tablePrefix   前缀
     * @param autoRemovePre 是否去除前缀
     * @return
     */
    default String tableToJava(String tableName, String tablePrefix, boolean autoRemovePre) {
        if (autoRemovePre) {
            tableName = tableName.substring(tableName.indexOf("_") + 1);
        }
        if (StringUtils.hasLength(tablePrefix)) {
            tableName = tableName.replace(tablePrefix, "");
        }

        return columnToJava(tableName);
    }

    default String columnToJava(String columnName) {
        return SqlColumnConvertUtil.convertToClassType(columnName);
    }

}
